<?php

/*
 * @author Khanh Nam
 * @email nam.trankhanh.vn@gmail.com
 */

class Forcehttps extends CBehavior
{
    public function attach($owner)
    {
        $owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest'));
    }
    public function handleBeginRequest($event)
    {
        if ( !Yii::app()->getRequest()->isSecureConnection ) {
            if(defined('PRODUCTION_SERVER')){
                # Redirect to the secure version of the page.
                $url = 'https://' .
                    Yii::app()->getRequest()->serverName .
                    Yii::app()->getRequest()->requestUri;
                    Yii::app()->request->redirect($url);           
            }
        }
    }
}
?>